"""
This file creates your application.
"""

from os.path import abspath, dirname, join
import sys, os

packages_dir = "packages"
application_dir = abspath(join(dirname(__file__), '..'))
package_dir_path = join(application_dir, packages_dir)

sys.path.insert(0, package_dir_path)

# Append zip archives to path for zipimport
for filename in os.listdir(package_dir_path):
    if filename.endswith((".zip", ".egg")):
        sys.path.insert(0, "%s/%s" % (package_dir_path, filename))

from flask import Flask
from gae_mini_profiler import profiler, templatetags
from werkzeug.debug import DebuggedApplication

import settings

app = Flask(__name__)
app.config.from_object(settings)

# Enable jinja2 loop controls extension
app.jinja_env.add_extension('jinja2.ext.loopcontrols')

@app.context_processor
def inject_profiler():
    return dict(profiler_includes=templatetags.profiler_includes())

# Load url dispatch routes
import urls

# Werkzeug Debugger (only enabled when DEBUG=True)
if app.debug:
    app = DebuggedApplication(app, evalex=True)

# GAE Mini Profiler (only enabled on dev server)
app = profiler.ProfilerWSGIMiddleware(app)